<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>获取数组最大值</title>
</head>

<body>

</body>
<script>
  "use strict"; {
    // 获取数组中的最大值
    let arr = [33, 11, 0, 233, 12];
    let calc_max = (list) => {
      return list.reduce((pre, value, index, arr) => {
        return pre > value ? pre : value;
      });
    };
    console.log(calc_max(arr));
  }; {
    let arr = [
      { name: "iphone", price: 10 },
      { name: "华为", price: 12 },
      { name: "小米", price: 1270 },
      { name: "雪梨", price: 111 },
    ];
    // 获取数组(购物车)中最贵的商品
    let calc_max = (list) => {
      return list.reduce((pre, v) => pre.price > v.price ? pre : v);
    };
    console.log(calc_max(arr), "====");

    // 获取价格超过一万元的商品名称和价格
    let get_price = (list) => {
      return list.reduce((pre, value, index, arr) => {
        if (value.price > 1000) pre.push(value);
        return pre;
      }, []);
    };
    // console.table(get_price(arr));

    // 获取价格超过一万元的商品名称(不要价格)
    let get_name = (list) => {
      return list.reduce((pre, value, index, arr) => {
        if (value.price > 1000) pre.push(value);
        return pre;
      }, []).map((item, key, pre_arr) => {
        // 此时只返回商品价格
        return item.name;
      });
    };
    console.table(get_name(arr));
  }; {
    // 去除重复的商品
    let cart = [
      { name: "小米", price: 1990 },
      { name: "小米", price: 14790 },
      { name: "苹果", price: 1790 },
      { name: "华为", price: 790 },
    ];
    const filter_goods = (goods) => {
      return goods.reduce((pre, value, index, arr) => {
        //数组pre里面的每一项的name是否与当前value的name相等
        let find = pre.find((item, key, pre_arr) => {
          return item.name == value.name;
        });
        // 若pre的数组里面不存在了，则添加该项
        if (!find) pre.push(value);
        // 最把pre返回
        return pre;
      }, []);
    };
    console.table(filter_goods(cart));

  }
</script>

</html>